package com.wxxymaker.controller;

import com.wxxymaker.model.entity.Shop;
import com.wxxymaker.model.entity.Stage;
import com.wxxymaker.model.table.ShopStageCountTable;
import com.wxxymaker.model.table.StageShopStarTable;
import com.wxxymaker.service.ShopService;
import com.wxxymaker.service.StageCountService;
import com.wxxymaker.service.StageService;
import com.wxxymaker.utils.web.ResponseWarp;
import com.wxxymaker.utils.web.StageCountUtil;
import com.wxxymaker.utils.web.StageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: Soft
 * @Date: 2018/5/18 19:56
 * @Desc: 用于返回制图相关的Json数据
 */
@RestController
@RequestMapping("/table")
public class TableHandler {

    @Autowired
    private StageService stageService;
    /*
     * 获取公司信息用来制作图表，主页的表。公司阶段评价统计图
     * @param type 公司类型 默认物业公司
     *
    @PostMapping("/shops")
    public ResponseWarp shops(@RequestParam(defaultValue = "1") Integer type) {
        ShopStageCountTable table = new ShopStageCountTable();

        List<Stage> stages = stageService.page(1, Integer.MAX_VALUE,
                StageUtil.getTypesByShop(type)).getContent();
        table.setStages(stages);
        List<Shop> shops =  null;
        if (type == 1) {
            shops = shopService.page(1, Integer.MAX_VALUE, Shop.TypeDim.WUYE).getContent();
        } else {
            shops = shopService.page(1, Integer.MAX_VALUE, Shop.TypeDim.STUST,
                    Shop.TypeDim.TEAST).getContent();
        }
        for (Shop shop : shops) {
            ShopStageCountTable.ShopInfo shopInfo = new ShopStageCountTable.ShopInfo();
            shopInfo.setShop(shop);
            shopInfo.setStageCounts(stageCountService.findAllByShop(shop));

            table.getShopInfos().add(shopInfo);
        }
        return ResponseWarp.ok(table);
    }
    */

    /**
     * 获取该阶段下，所有公司的打分星级计数 数据
     * @param id 阶段id
     */
    @PostMapping("/stage/{id}")
    public ResponseWarp stage(@PathVariable Integer id) {
        StageShopStarTable shopStarTable = new StageShopStarTable();

        Stage      stage = stageService.getOne(id);
        List<Shop> shops = stageService.getShops(stage);

        for (Shop shop : shops) {
            StageShopStarTable.ShopStar shopStar = new StageShopStarTable.ShopStar();
            shopStar.setShop(shop);
            shopStar.setStars(stageService.countStarNumForShop(stage, shop));
            shopStar.setScore(StageCountUtil.countScore(shopStar.getStars()).getTotal());

            shopStarTable.getShopStars().add(shopStar);
        }

        shopStarTable.setStage(stage);
        return ResponseWarp.ok(shopStarTable);
    }
}
